package com.pajk.user.service.common.idpool;

/**
 * interface for getting ids
 *
 * @author Haomin Liu
 */
public interface IDPool {

    /**
     * borrow an id from current pool
     * then you may:
     * 1. return this id back by "giveback" method to pool if your transaction rolled back, then this id could be reused next time;
     * 2. or consume it by "consume" method if your transaction is successful.
     *
     * @return id
     * @throws java.lang.RuntimeException if id pool ran out(may caused by number of reasons, detailed in exeption message)
     */
    String borrow();

    /**
     * return an unused id, then this id can be reallocated later
     *
     * @param id id to be returned
     */
    void giveback(String id);

    /**
     * officially consumes this id
     *
     * @param id id to be consumed
     */
    void consume(String id);

    String get();
}
